import { IRootState } from '@/store/types'
import { Module } from 'vuex'

import { ISystemState } from './types'
import {
  getPageListData,
  deletePageData,
  createPageData,
  editPageData
} from '@/network/main/system/system'

const systemModule: Module<ISystemState, IRootState> = {
  namespaced: true,
  state: () => ({
    queryInfo: {
      offset: 1,
      size: 5
    },
    usersList: [],
    usersCount: 0,
    roleList: [],
    roleCount: 0,
    goodsList: [],
    goodsCount: 0,
    menuList: [],
    menuCount: 0
  }),
  getters: {
    pageListData(state) {
      return (pageName: string) => {
        return state[`${pageName}List` as keyof typeof state]
      }
    },
    pageListCount(state) {
      return (pageName: string) => {
        return state[`${pageName}Count` as keyof typeof state]
      }
    }
  },
  mutations: {
    saveQueryInfo(state, queryInfo) {
      state.queryInfo = queryInfo
    },
    changeUsersList(state, userList: any[]) {
      state.usersList = userList
    },
    changeUsersCount(state, userCount: number) {
      state.usersCount = userCount
    },
    changeRoleList(state, rolelist: any[]) {
      state.roleList = rolelist
    },
    changeRoleCount(state, userCount: number) {
      state.roleCount = userCount
    },
    changeGoodsList(state, goodsList: any[]) {
      state.goodsList = goodsList
    },
    changeGoodsCount(state, goodsCount: number) {
      state.goodsCount = goodsCount
    },
    changeMenuList(state, menuList: any[]) {
      state.menuList = menuList
    },
    changeMenuCount(state, menuCount: number) {
      state.menuCount = menuCount
    }
  },
  actions: {
    async getPageListAction({ commit }, payload: any) {
      // 1.获取pageUrl
      const pageName = payload.pageName
      let pageUrl = ''
      switch (pageName) {
        case 'users':
          pageUrl = '/users/list'
          break
        case 'role':
          pageUrl = '/role/list'
          break
        case 'goods':
          pageUrl = '/goods/list'
          break
        case 'menu':
          pageUrl = '/menu/list'
          break
      }
      // 2.对页面发起请求
      const pageResult = await getPageListData(pageUrl, payload.queryInfo)
      const { list, totalCount } = pageResult.data

      commit(`change${pageName.toUpperCase()[0] + pageName.slice(1)}List`, list)
      commit(
        `change${pageName.toUpperCase()[0] + pageName.slice(1)}Count`,
        totalCount
      )
      commit('saveQueryInfo', payload.queryInfo)
    },
    async deletePageDataAction({ dispatch, state }, payload: any) {
      // 1. 获取pageName和id
      // 1.pageName -> /users
      // 2.id -> /users/id
      const { pageName, id } = payload
      const pageUrl = `/${pageName}/${id}`
      // 2.调用删除网络请求
      await deletePageData(pageUrl)

      // 3.重新请求最新的数据

      dispatch('getPageListAction', {
        pageName,
        queryInfo: state.queryInfo
      })
    },
    async createPageDataAction({ dispatch, state }, payload) {
      // 1.创建数据的请求
      const { pageName, newData } = payload
      const pageUrl = `/${pageName}`
      await createPageData(pageUrl, newData)
      // 2.请求最新的数据
      dispatch('getPageListAction', {
        pageName,
        queryInfo: state.queryInfo
      })
    },
    async editPageDataAction({ dispatch, state }, payload) {
      // 1.修改数据的请求
      const { pageName, editData, id } = payload
      const pageUrl = `/${pageName}/${id}`
      await editPageData(pageUrl, editData)
      // 2.请求最新的数据
      dispatch('getPageListAction', {
        pageName,
        queryInfo: state.queryInfo
      })
    }
  }
}

export default systemModule
